<?php
	include "./utils/auth.php";
?>







<div class="centercontainer">
	<div class="centercontainerheader">
		CW Stats for the last 2 Months
	</div>
	<div class="centercontaineritem">
		<div id="memberstable">
			<table width="100%">
				<thead>
					<tr>
						<td></td>
						<td>Name</td>
						<td colspan="2">Played Wars</td>
						<td colspan="2">Attended Battles</td>
					</tr>
					<tr>
						<td></td>
						<td></td>
						<td>%</td>
						<td>#</td>
						<td>%</td>
						<td>#</td>
						
				</thead>
				<tbody>
					<?php
					//total battles
					if(!($tstmt = $db->prepare("
					SELECT 
						COUNT(DISTINCT channelid)
					FROM wottools.wotcwchannel
						WHERE `timestamp` > DATE_SUB(now(), INTERVAL 2 MONTH) ")))
					{
						echo "error: ".$db->error;
					}
					$tstmt->bind_result($battles2months);
					$tstmt->execute();
					$tstmt->fetch();
					$tstmt->close();
					echo "".$battles2months."<br>";
					
					
					
					if(!($stmt = $db->prepare("
					SELECT id,wotaccountname, COUNT(DISTINCT userid,c) as played2months,  COUNT(DISTINCT ui2,res) as reserve2months

					FROM wottools.users
						LEFT JOIN 
						(
							SELECT DISTINCT userid, concernedchannel as c
							FROM wottools.combatants
							WHERE reserve = 0
						) as keks
						ON (keks.userid = id) 
						
						LEFT JOIN
						(
							SELECT DISTINCT userid as ui2, concernedchannel as res
							FROM wottools.combatants
							WHERE reserve = 1
						) as keks2
						ON (keks2.ui2 = id)
					WHERE inclan=1
					GROUP BY id
					ORDER BY played2months desc, reserve2months desc
					")))
					{
						echo "error: ".$db->error;
					}
					$stmt->bind_result($id,$name,$played2months,$reserve2months);
					$stmt->execute();
					//$stmt->store_result();
					$i = 1;
					while($stmt->fetch())
					{
						echo '<tr>';
						echo '<td>'.$i.'</td>';
						echo '<td>'.$name.'</td>';
						echo '<td>'.number_format($played2months/$battles2months*100).'%</td>';
						echo '<td>'.$played2months.'</td>';
						echo '<td>'.number_format(($reserve2months+$played2months)/$battles2months*100).'%</td>';
						echo '<td>'.($played2months+$reserve2months).'</td>';
						echo '</tr>';
						$i++;
					}
					//$stmt->free_result();
					$stmt->close();
					?>
				</tbody>
			</table>
		</div>
	</div>
</div>
		